package com.jiuli.liping.service.impl;

import com.jiuli.liping.dao.MySystemDao;
import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.dao.ProductOrderDao;
import com.jiuli.liping.util.DateUtils;
import com.jiuli.liping.util.JaxWsDynamicClientFactory;
import com.jiuli.liping.util.XmlUtil;
import com.jiuli.liping.vo.*;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.jiuli.liping.service.MySystemService;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.namespace.QName;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.List;

/**
 * Author zyl
 * Date  2020-03-14
 */
@Service
public class MySystemServiceImpl implements MySystemService {

    public final static String oa_url = "http://192.168.5.20:99/services/WorkflowServiceXml?wsdl";
    @Autowired
    private MySystemDao mySystemDao;

    @Autowired
    private ProductOrderDao productOrderDao;

    @Autowired
    private ProductDao productDao;
    
    @Override
    public MySystem get(String id){
        return mySystemDao.get(id);
    }
    
    @Override
    public List<MySystem> findList(MySystem mySystem) {
        return mySystemDao.findList(mySystem);
    }
    
    @Override
    public List<MySystem> findAllList() {
        return mySystemDao.findAllList();
    }
    
    @Override
    public int insert(MySystem mySystem) {
        return mySystemDao.insert(mySystem);
    }
    
    @Override
    public int insertBatch(List<MySystem> mySystems){
        return mySystemDao.insertBatch(mySystems);
    }
    
    @Override
    public int update(MySystem mySystem) {
        return mySystemDao.update(mySystem);
    }
    
    @Override
    public int delete(MySystem mySystem) {
        return mySystemDao.delete(mySystem);
    }

    @Override
    public List<MySystem> getProductList() {
        return mySystemDao.getProductList();
    }

    /**
     * 定时任务，订单超过半小时自动作废
     * 方法关闭
     */
    @Transactional
   // @Scheduled(cron = "0 0/1 * * * ? ")
    public int productOrderZf(){
        //找到所有提交的订单，超过半小时给他改变状态
        Timestamp curDate = new Timestamp(System.currentTimeMillis());
        ProductOrder po = new ProductOrder();
        po.setOrderStatus("3");
        long a=0;
        List<NumberOAUser> oaOrder = productOrderDao.getOaOrder();
            if (oaOrder != null) {
                for (int i = 0; i < oaOrder.size(); i++) {
                    a = DateUtils.getTimeDifference3(curDate, oaOrder.get(i).getCreateTime());

                    if (a-240>0) {
                        String s = oaOrder.get(i).getOanumber();
                        if (!s.equals("")) {
                            try {
                                rejectRequest(Integer.parseInt(oaOrder.get(i).getOanumber()), Integer.parseInt(oaOrder.get(i).getUserid()));
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }



        List<ProductOrder> list = productOrderDao.findAllListByDd(po);
        for (ProductOrder pro: list) {
            a = DateUtils.getTimeDifference3(curDate, pro.getCreateTime());
            System.out.println(a);
            System.out.println(curDate);
            System.out.println(pro.getCreateTime());
            if(a-240>0){
                pro.setOrderStatus("5");//超过4小时冻结订单
                productOrderDao.updateStatus(pro);
                //存货数量返回
                int iceProductNumber = productDao.getIceProductNumber(pro.getFbk1());
                System.out.println("定时冻结数量:" + iceProductNumber+"商品编号:"+pro.getFbk1());
                int i = iceProductNumber - Integer.parseInt(pro.getAmount());
                System.out.println("定时解冻完成后数量:" + i+"商品编号:"+pro.getFbk1());
                productDao.addNumber(pro.getFbk1(),i+"");
            }else {
                System.out.println(a);
            }
        }



        return 0;
    }

    public static void rejectRequest(int requestid,int userid) throws RemoteException
    {
        String url = oa_url;
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        Client client = dcf.createClient(url);

        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(30000); //连接超时时间
        policy.setReceiveTimeout(180000);//请求超时时间.
        conduit.setClient(policy);

        System.out.println("返回数据4:" + client);
        QName name = new QName("webservices.services.weaver.com.cn", "getWorkflowRequest");
        Object[] objects=new Object[5];
        objects[0]=1;

        try {

            // System.out.println("发送数据:" + requestInfo);
            objects = client.invoke(name,requestid,userid,0);
            System.out.println("返回数据:" + objects[0]);

            QName name1 = new QName("webservices.services.weaver.com.cn", "submitWorkflowRequest");
            objects =client.invoke(name1,objects[0]+"", requestid, 1400 ,"reject","退回流程"+new java.util.Date());
            System.out.println("驳回数据:" + objects[0]);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}
